<template>
	<view class="content">
		<view class="row" style="border-bottom: 1px solid #C8C7CC;padding-bottom: 5px;">
			<view>账号:</view>
			<input style="margin-left: 5px;" v-model="userName"></input>
		</view>
		<view class="row" style="border-bottom: 1px solid #C8C7CC;padding-bottom: 5px;margin-top: 25px;">
			<view>密码:</view>
			<input type="password" style="margin-left: 5px;" v-model="password"></input>
		</view>
		<view class="row ju-c al-c" style="margin-top: 250px;">
			<button class="lg-btn" @click="btnLogin()">登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'login',
				userName: '',
				code: '',
				password: '',
			}
		},
		onLoad(query) {
			if (query.code) {
				this.code = query.code
				this.toLogin(query.code)
			} else {
				this.getWechatData()
			}
		},
		methods: {
			toIndex() {
				uni.navigateTo({
					url: '/pages/index/index'
				});
			},
			btnLogin() {
				var that = this

				uni.request({
					url: getApp().globalData.baseUrl + '/api/system/login',
					data: {
						sysType: 1,
						openId: uni.getStorageSync('openId'),
						password: this.password,
						userName: this.userName,
					},
					method: 'POST',
					success: (res) => {
						if (res.data.statusCode == 200) {
							uni.setStorageSync('account', res.data.data.account)
							uni.setStorageSync('fullName', res.data.data.fullName)
							uni.setStorageSync('accessToken', res.data.data.accessToken)
							uni.setStorageSync('refreshToken', res.data.data.refreshToken)
							uni.navigateTo({
								url: '/pages/index/index'
							});
						} else {
							uni.showToast({
								title: res.data.errors,
								icon: 'none',
								mask: true,
								duration: 1000
							});
						}
					}
				})
			},
			toLogin(code) {
				var that = this
				uni.request({
					url: getApp().globalData.baseUrl + '/api/system/login',
					data: {
						sysType: 1,
						code: code
					},
					method: 'POST',
					success: (res) => {
						if (res.data.statusCode == 200) {
							if (res.data.data.accessToken) { //直接登录			
								uni.setStorageSync('account', res.data.data.account)
								uni.setStorageSync('fullName', res.data.data.fullName)
								uni.setStorageSync('accessToken', res.data.data.accessToken)
								uni.setStorageSync('refreshToken', res.data.data.refreshToken)
								uni.navigateTo({
									url: '/pages/index/index'
								});
							} else {
								uni.setStorageSync('openId', res.data.data.openId)
							}
						}
					}
				})
			},
			getWechatData() {
				//location.href = "http://localhost:8080/?code=string"
				location.href =
					'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + getApp().globalData.wechatAppId +
					'&redirect_uri=' + getApp().globalData.redirect_uri +
					'&response_type=code&scope=snsapi_base'
			},
		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 25px 15px;
	}

	.lg-btn {
		height: 50px;
		width: 80%;
		background-color: #007AFF;
		color: #fff;
		text-align: center;
		line-height: 50px;
		border-radius: 25px;
	}
</style>
